﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserMofForm : Form
    {
        public UserMofForm()
        {
            InitializeComponent();
        }

        private bool modifycheck()
        {
            if (this.checkBox1.Checked)
            {
                if (User.Userrole != "系统管理员" && this.textBox9.Text == User.Password)
                {
                    MessageBox.Show("新密码和旧密码不可以一样,请重新输入密码");
                    return false;
                }

                if (this.textBox9.Text != this.textBox10.Text)
                {
                    MessageBox.Show("密码和确认密码不一致,请重新输入密码");
                    return false;
                }
            }
            
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名");
                return false;
            }
            if (this.comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择性别");
                return false;
            }
            if (this.comboBox2.SelectedItem == null)
            {
                MessageBox.Show("请选择用户权限");
                return false;
            }
            return true;
        }


        private void displayuser()
        {
            int userid = Int32.Parse(this.textBox1.Text);
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            var query = from c in dbcontext.tb_user
                        where c.ID == userid
                        select c;

            if (query.Count() == 0)
            {
                MessageBox.Show("查询的用户号码不存在");
                return;
            }

            this.textBox2.Text = query.First().Name;
            switch (query.First().sex)
            {
                case "男":
                    this.comboBox1.SelectedIndex = 0;
                    break;
                case "女":
                    this.comboBox1.SelectedIndex = 1;
                    break;
            }

            switch (query.First().Role)
            {
                case "系统管理员":
                    this.comboBox2.SelectedIndex = 0;
                    break;
                case "系统操作员":
                    this.comboBox2.SelectedIndex = 1;
                    break;
                case "系统用户":
                    this.comboBox2.SelectedIndex = 2;
                    break;
            }

            this.textBox3.Text = query.First().IDcard;
            this.dateTimePicker1.Checked = false;
            if (query.First().Birthday.ToString() != "")
            {
                this.dateTimePicker1.Value = DateTime.Parse(query.First().Birthday.ToString());
            }
            this.textBox4.Text = query.First().Phone;
            this.textBox5.Text = query.First().email;
            this.textBox6.Text = query.First().Workaddress;
            this.textBox7.Text = query.First().Homeaddress;
            this.textBox8.Text = query.First().Comment;
            this.checkBox1.Checked = false;

        }

        private void modifyuser()
        {
            int userid = Int32.Parse(this.textBox1.Text);
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            var query = from c in dbcontext.tb_user
                        where c.ID == userid
                        select c;

            if (query.Count() == 0)
            {
                MessageBox.Show("需要更新的用户号码不存在");
                return;
            }

            if (modifycheck() == false)
            {
                return;
            }

            tb_user item = dbcontext.tb_user.First(c => c.ID == userid);

            string[] modifydataorg = new string[12];
            string[] modifydatadest = new string[12];

            modifydataorg[0] = item.ID.ToString();
            modifydataorg[1] = item.Name;
            modifydataorg[2] = item.Password = item.Password;
            modifydataorg[3] = item.sex;
            modifydataorg[4] = item.Role;
            modifydataorg[5] = item.IDcard == null ? "无" : item.IDcard;
            modifydataorg[6] = item.Birthday == null ? "无" : item.Birthday.ToString();
            modifydataorg[7] = item.Phone == null ? "无" : item.Phone;
            modifydataorg[8] = item.email == null ? "无" : item.email;
            modifydataorg[9] = item.Workaddress == null ? "无" : item.Workaddress;
            modifydataorg[10] = item.Homeaddress == null ? "无" : item.Homeaddress;
            modifydataorg[11] = item.Comment == null ? "无" : item.Comment;



            item.Name = this.textBox2.Text;
            item.sex = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            item.Role = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();

            if (this.checkBox1.Checked)
            {
                item.Password = this.textBox9.Text;
            }
            
            if (this.textBox3.Text != "")
            {
                item.IDcard = this.textBox3.Text;
            }

            if (this.dateTimePicker1.Checked)
            {
                item.Birthday = this.dateTimePicker1.Value;
            }

            if (this.textBox4.Text != "")
            {
                item.Phone = this.textBox4.Text;
            }

            if (this.textBox5.Text != "")
            {
                item.email = this.textBox5.Text;
            }

            if (this.textBox6.Text != "")
            {
                item.Workaddress = this.textBox6.Text;
            }

            if (this.textBox7.Text != "")
            {
                item.Homeaddress = this.textBox7.Text;
            }

            if (this.textBox8.Text != "")
            {
                item.Comment = this.textBox8.Text;
            }

            modifydatadest[0] = this.textBox1.Text;
            modifydatadest[1] = this.textBox2.Text;
            modifydatadest[2] = item.Password;
            modifydatadest[3] = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            modifydatadest[4] = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();
            modifydatadest[5] = this.textBox3.Text == "" ? "无" : this.textBox3.Text;
            modifydatadest[6] = this.dateTimePicker1.Checked == false ? "无" : this.dateTimePicker1.Value.ToString();
            modifydatadest[7] = this.textBox4.Text == "" ? "无" : this.textBox4.Text;
            modifydatadest[8] = this.textBox5.Text == "" ? "无" : this.textBox5.Text;
            modifydatadest[9] = this.textBox6.Text == "" ? "无" : this.textBox6.Text;
            modifydatadest[10] = this.textBox7.Text == "" ? "无" : this.textBox7.Text;
            modifydatadest[11] = this.textBox8.Text == "" ? "无" : this.textBox8.Text;

            string detail = UserLog.builddetailstring(UserLog.usermanagermodifyheader, "修改", modifydatadest, modifydataorg);

            try
            {
                dbcontext.SubmitChanges();
                UserLog.addusermanagerecord(User.UserID, User.Username, "修改", item.ID, item.Name, "成功", detail);
                MessageBox.Show("修改用户成功");
            }
            catch (Exception ex)
            {
                UserLog.addusermanagerecord(User.UserID, User.Username, "修改", item.ID, item.Name, "失败", detail);
                MessageBox.Show("修改用户失败");
            }


        }



        private void button1_Click(object sender, EventArgs e)
        {
            modifyuser();
        }





        private void UserAddForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Checked = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            displayuser();
        }
    }
}
